package com.ipan.kits.mapper;

import java.util.List;
import java.util.Map;

import com.ipan.kits.base.ExceptionUtil;

/**
 * JsonMapper接口
 * 不同项目依赖的Json实现可能是不同的，为了兼容Jackson与Fastjson，所以，提取了该接口；
 * 
 * @author iPan
 * @version 2019-06-03
 */
public abstract class JsonMapper {
	
	private static JsonMapper _MAPPER = null;
	
	public abstract String toJson(Object object);
	
	public abstract <T> T fromJson(String jsonString, Class<T> clazz);
	
	public abstract Map<String, Object> jsonToMap(String jsonString);
	
	public abstract List<Map<String, Object>> jsonToList(String jsonString);
	
	public abstract String toJsonP(String functionName, Object object);
	
	public static JsonMapper getDefaultJsonMapper() {
		if (_MAPPER != null) {
			return _MAPPER;
		}
		synchronized(JsonMapper.class) {
			if (_MAPPER != null) {
				return _MAPPER;
			}
			try {
				Class.forName("com.alibaba.fastjson.JSON");
				_MAPPER = FastJsonMapper.me();
			} catch (ClassNotFoundException e) {
				try {
					Class.forName("com.fasterxml.jackson.databind.ObjectMapper");
					_MAPPER = JacksonMapper.nonNullMapper();
				} catch (ClassNotFoundException e1) {
					ExceptionUtil.unchecked(e1);
				}
			}
		}
		return _MAPPER;
	}
	
}
